<template>
  <div class="app-container">
    <el-form ref="dataForm" :rules="rules" :model="temp" label-position="left" label-width="150px"
             style="width: 100%; padding: 0 30px;">

      <el-row :gutter="20">
<!--        <el-col :span="12">
          <el-form-item label="deptLogo" prop="deptLogo">
            <el-input v-model="temp.deptLogo"/>
          </el-form-item>
        </el-col>-->
        <el-col :span="12">
          <el-form-item label="名称" prop="name">
            <el-input v-model="temp.name"/>
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="地址" prop="address">
            <el-input v-model="temp.address"/>
          </el-form-item>
        </el-col>
        <!--<el-col :span="12">
          <el-form-item label="govCmpy" prop="govCmpy">
            <el-input v-model="temp.govCmpy"/>
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="level" prop="level">
            <el-input v-model="temp.level"/>
          </el-form-item>
        </el-col>-->
        <el-col :span="12">
          <el-form-item label="法人" prop="mhLegalPerson">
            <el-input v-model="temp.mhLegalPerson"/>
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="联系人" prop="mhLinkman">
            <el-input v-model="temp.mhLinkman"/>
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="联系电话" prop="mhPhone">
            <el-input v-model.number="temp.mhPhone"/>
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="邮箱" prop="mhEmail">
            <el-input v-model="temp.mhEmail"/>
          </el-form-item>
        </el-col>
<!--        <el-col :span="12">
          <el-form-item label="mhBizLicenseSignpic" prop="mhBizLicenseSignpic">
            <el-input v-model="temp.mhBizLicenseSignpic"/>
          </el-form-item>
        </el-col>-->
<!--        <el-col :span="12">
          <el-form-item label="mhType" prop="mhType">
            <el-input v-model="temp.mhType"/>
          </el-form-item>
        </el-col>-->
<!--        <el-col :span="12">
          <el-form-item label="mhStatus" prop="mhStatus">
            <el-input v-model="temp.mhStatus"/>
          </el-form-item>
        </el-col>-->
<!--        <el-col :span="12">
          <el-form-item label="父ID" prop="parentId">
            <el-input v-model="temp.parentId"/>
          </el-form-item>
        </el-col>-->

<!--        <el-col :span="12">
          <el-form-item label="编号" prop="number">
            <el-input v-model="temp.number"/>
          </el-form-item>
        </el-col>-->
<!--        <el-col :span="12">
          <el-form-item label="部门类型" prop="tmptDeptType">
            <select-option
              v-model="temp.tmptDeptType"
              :clearable=false
              prop-name="TmptDeptType"
              :prop-ds-type=1
              prop-url="/citymh/enum/getData4AddBy"
            />
          </el-form-item>
        </el-col>-->
<!--        <el-col :span="12">
          <el-form-item label="删除标志" prop="deleteflag">
            <select-option
              v-model="temp.deleteflag"
              :clearable=false
              prop-name="Deleteflag"
              :prop-ds-type=1
              prop-url="/citymh/enum/getData4AddBy"
            />
          </el-form-item>
        </el-col>-->
      </el-row>
    </el-form>
  </div>
</template>

<script>
  import { createApi, updateApi, fetchList } from './api'
  // 导入多选项组件
  import SelectOption from '@/views/coding/components/SelectOption'

  export default {
    name: 'TmptAuthDept-FormDetail',

    components: { SelectOption },

    props: {
      isModule: {
        type: Boolean,
        default: false
      },
      row: {}
    },

    data() {
      var checkMobile = (rule,value,cb)=>{
        const regMobile =  /^[1][3,4,5,7,8][0-9]{9}$/;
        if(regMobile.test(value)){
          return cb()
        }
        cb(new Error("只能输入11位手机号"))
      };
      return {
        rules: {
          // deptLogo: [{ required: true, message: 'required', trigger: 'blur' }],
          address: [{ required: true, message: 'required', trigger: 'blur' }],
          // govCmpy: [{ required: true, message: 'required', trigger: 'blur' }],
          // level: [{ required: true, message: 'required', trigger: 'blur' }],
          mhLegalPerson: [{ required: true, message: 'required', trigger: 'blur' }],
          mhLinkman: [{ required: true, message: 'required', trigger: 'blur' }],
          mhPhone: [{ required: true, message: 'required', trigger: 'blur' }, {validator:checkMobile,trigger: 'blur'}],
          mhEmail: [{ required: true, message: 'required', trigger: 'blur' }],
          // mhBizLicenseSignpic: [{ required: true, message: 'required', trigger: 'blur' }],
          // mhType: [{ required: true, message: 'required', trigger: 'blur' }],
          // mhStatus: [{ required: true, message: 'required', trigger: 'blur' }],
          // parentId: [{ required: true, message: 'required', trigger: 'blur' }],
          name: [{ required: true, message: 'required', trigger: 'blur' }],
          // number: [{ required: true, message: 'required', trigger: 'blur' }],
          // tmptDeptType: [{ required: true, message: 'required', trigger: 'blur' }],
          // deleteflag: [{ required: true, message: 'required', trigger: 'blur' }]
        },
        temp: {}
      }
    },

    watch: {
      'row': {
        immediate: true, // vue首次赋值不触发watch的解决方法
        handler: function(nval) {
          if (this.isModule) {
            // this.temp = Object.assign({}, this.row) // copy obj
            if (this.isNull(nval)) {
              return
            }
            if (this.isNull(nval.id)) {
              return
            }
            this.getList(nval.id)
          }
        }
      }
    },

    methods: {
      getList(_id) {
        this.resetTemp()
        fetchList({ id: _id }).then(result => {
          if (!result.success) {
            this.notify(result)
            return
          }
          this.temp = result.map.rows[0]
        })
      },
      // 给外部调用，清空form
      resetTemp() {
        this.temp = {
          deptLogo: '',
          address: '',
          govCmpy: '',
          level: '',
          mhLegalPerson: '',
          mhLinkman: '',
          mhPhone: '',
          mhEmail: '',
          mhBizLicenseSignpic: '',
          mhType: '',
          mhStatus: '',
          parentId: '',
          name: '',
          number: '',
          tmptDeptType: '',
          deleteflag: '',
          id: undefined
        }
      },
      // 给外部调用，设置默认值，请根据实际情况修改id值
      defaultTemp() {
        this.temp = {
          tmptDeptType: 0, //设置默认值
          deleteflag: 0 //设置默认值
        }
      },
      // 树表： 拿当前节点ID作为父节点ID，建立树关系
      setParentNodeId(id) {
        // this.temp.tmptAuthDeptId = id
        this.temp.parentId = id
      },
      // 给外部调用，插入记录
      createData() {
        this.$refs['dataForm'].validate((valid) => {
          if (valid) {
            const param = Object.assign({}, this.temp)
            createApi(param).then((r) => {
              this.notify(r)
              if (r.success) {
                this.resetTemp()
                // 从父组件调用此方法，进行回调下
                if (this.isModule) {
                  this.$emit('createResult')
                }
              }
            })
          }
        })
      },
      // 给外部调用，修改记录
      updateData() {
        this.$refs['dataForm'].validate((valid) => {
          if (valid) {
            // columnsTag.column4Add
            const param = { id: this.temp.id }
            param.deptLogo = this.temp.deptLogo
            param.address = this.temp.address
            param.govCmpy = this.temp.govCmpy
            param.level = this.temp.level
            param.mhLegalPerson = this.temp.mhLegalPerson
            param.mhLinkman = this.temp.mhLinkman
            param.mhPhone = this.temp.mhPhone
            param.mhEmail = this.temp.mhEmail
            param.mhBizLicenseSignpic = this.temp.mhBizLicenseSignpic
            param.mhType = this.temp.mhType
            param.mhStatus = this.temp.mhStatus
            param.parentId = this.temp.parentId
            param.name = this.temp.name
            param.number = this.temp.number
            param.tmptDeptType = this.temp.tmptDeptType
            param.deleteflag = this.temp.deleteflag
            updateApi(param).then((r) => {
              this.notify(r)
              if (r.success) {
                this.resetTemp()
                // 从父组件调用此方法，进行回调下
                if (this.isModule) {
                  this.$emit('updateResult')
                }
              }
            })
          }
        })
      },
      // 给外部调用，清除校验
      clearValidate() {
        this.$refs['dataForm'].clearValidate()
      }
    }

  }
</script>
<style>

</style>
